// pages/detail/detail.js
var WxParse = require('../../wxParse/wxParse.js');
var utils = require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:[],
    iconType: [
      'clear'//关闭的icon
    ],
    gid: '',//商品ID
    imgUrls: [],//图片轮播
    goods: [],//商品信息
    specList:[],
    specChecked:false,//选中商品规格ID
    totalFee:0,//规格选择时现实的费用
    totalCount:1,//选中商品数量
    tk: false,//商品规格弹窗
    nodes: [{
      name: 'div',
      attrs: {
        class: 'div_class',
        style: 'line-height: 60px; color: red;'
      },
      children: [{
        type: 'text',
        text: 'Hello&nbsp;World!'
      }]
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    this.setData({
      gid:options.id
    })
    this.getGoodsInfo()
    this.setData({
      userInfo:app.globalData.userInfo
    })
  },
  /**
   * 获取商品信息
   */
  getGoodsInfo:function () {
    var _this = this
    wx.request({
      url: app.globalData.requestip+"&c=index&a=getGoodsDetails",
      data:{
        gid:_this.data.gid
      },
      success:function(res) {
        //使用三方插件，将富文本内容转化文小程序可识别格式
        WxParse.wxParse('article', 'html', res.data.goods.content, _this, 5);
        _this.setData({
          imgUrls:res.data.imgUrls,
          goods:res.data.goods,
          specList: res.data.specList,
          totalFee: res.data.goods.price
        })
      },
      fail:function (res) {
        utils.loadRequestFail(res)
      }
    })
  },

  /**
   * 显示/隐藏弹框
   */
  showFrame: function () {
    this.setData({
      tk: !this.data.tk
    })
  },

  /**
   * 规格选择
   */
  checkSpec: function (e) {
    var changed = {}
    var id = e.currentTarget.dataset.id;
    var checkPrice
    for (var i = 0; i < this.data.specList.length; i++) {
      if (id == this.data.specList[i].id) {
        changed['specList[' + i + '].checked'] = true
        changed['specChecked'] = this.data.specList[i]
      } else {
        changed['specList[' + i + '].checked'] = false
      }
    }
    this.setData(changed)
    this.getPrice()
  },

  /**
   * 增加数量
   */
  setIncNum:function () {
    var changed = {}
    var totalCount = parseInt(this.data.totalCount) + 1
    changed['totalCount'] = totalCount
    this.setData(changed)
    this.getPrice()
  },

  /**
   * 减少数量
   */
  setDecNum: function () {
    var changed = {}
    var totalCount = parseInt(this.data.totalCount) - 1
    if (totalCount >= 1) {
      changed['totalCount'] = totalCount
      this.setData(changed)
      this.getPrice()
    }
  },

  /**
   * 计算价格
   */
  getPrice: function () {
    var totalCount = this.data.totalCount
    var totalFee = this.data.totalFee
    var changed = {};

    //判断是否有选中规格
    if (this.data.specChecked) {
      changed['totalFee'] = parseFloat(this.data.specChecked['price']) * parseFloat(totalCount)
    }

    this.setData(changed)
  },

  /**
   * 添加到购物车
   */
  addCart:function () {
    var _this = this
    app.getUserInfo(function(cb){
      if (cb) {
        wx.request({
          url: app.globalData.requestip + "&c=cart&a=addCart",
          data: {
            gid: _this.data.goods.id,
            spec_id: _this.data.specChecked.id,
            count: _this.data.totalCount
          },
          header: app.globalData.header,
          success: function (res) {
            if (res.data.code == '1') {
              untils.loadFail(res)
            } else {
              wx.showToast({
                title: '加入成功',
                duration: 1000,
                mask: true,
                success: function (res) {
                  _this.setData({
                    tk: false
                  })
                },
                fail: function (res) { },
                complete: function (res) { },
              })
            }
          },
          fail: function (res) {
            untils.loadRequestFail(res)
          }
        })
      }
    })
  },

  /**
   * 获取购物车数量
   */
  getCartCount:function()
  {

  },

  /**
   * 跳转至购物车
   */
  tapCart:function () {
    wx.switchTab({
      url: '../cart/cart',
    })
  },

  /**
   * 跳转至首页
   */
  tapHome:function () {
    wx.switchTab({
      url: '../index/index',
    })
  },

  /**
   * 立即购买
   */
  tapBuy:function () {
    wx.showModal({
      title: '提示',
      content: '支付接口暂未开放',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})